<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div class="exception-page">
    <van-nav-bar 
      title="异常上报" 
      left-text="返回" 
      left-arrow 
      @click-left="onClickLeft"
      class="custom-nav" 
    />
    
    <div class="content-wrapper">
      <div class="upload-section">
        <h3 class="section-title">上传图片</h3>
        <p class="upload-tip">请上传清晰的故障照片，最多可上传9张</p>
        <div class="upload-area">
          <van-uploader 
            :after-read="onRead"
            :max-size="500 * 1024"
            :max-count="9"
            multiple
            accept="image/*"
            :preview-size="80"
          >
            <div class="upload-trigger">
              <van-icon name="photograph" size="24" />
              <span>添加图片</span>
            </div>
          </van-uploader>
        </div>
        
        <div class="image-preview">
          <template v-if="imageList.length > 0">
            <div v-for="(img, index) in imageList" 
                 :key="index" 
                 class="image-item"
                 :class="{ 'fade-in': true }"
            >
              <img :src="img" alt="异常图片">
              <div class="delete-btn" @click="deleteImage(index)">
                <van-icon name="cross" />
              </div>
            </div>
          </template>
        </div>
      </div>

      <div class="form-section">
        <h3 class="section-title">问题描述</h3>
        <van-cell-group inset>
          <van-field
            v-model="form.text"
            type="textarea"
            rows="4"
            placeholder="请详细描述您遇到的问题..."
            maxlength="200"
            show-word-limit
          />
        </van-cell-group>
      </div>
    </div>

    <div class="submit-section">
      <van-button 
        type="primary" 
        block 
        :disabled="!isFormValid"
        @click="handleSubmit"
      >
        提交异常报告
      </van-button>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { SentYichang, UploadImg } from '@/api/user'
import { showToast, showDialog } from 'vant'

const form = ref({
  images: '',
  text: ''
})

// 用于显示的图片列表
const imageList = computed(() => {
  return form.value.images ? form.value.images.split(',') : []
})

// 表单验证
const isFormValid = computed(() => {
  return imageList.value.length > 0 && form.value.text.trim().length > 0
})

const onRead = async (file) => {
  const formData = new FormData()
  formData.append('file', file.file)
  
  try {
    const res = await UploadImg(formData)
    if(form.value.images === ''){
      form.value.images = res.data.data
    }else{
      form.value.images = form.value.images + ',' + res.data.data
    }
    showToast({
      type: 'success',
      message: '上传成功'
    })
  } catch (error) {
    console.error('上传失败:', error)
    showToast({
      type: 'fail',
      message: '上传失败'
    })
  }
}

const handleSubmit = async () => {
  if (!isFormValid.value) {
    showToast('请填写完整信息')
    return
  }

  try {
    await showDialog({
      title: '确认提交',
      message: '确定要提交异常报告吗？',
      showCancelButton: true,
    })
    
    await SentYichang(form.value)
    showToast({
      type: 'success',
      message: '提交成功'
    })
    history.back()
  } catch (error) {
    if (error.cancel) return
    showToast({
      type: 'fail',
      message: '提交失败'
    })
  }
}

// 删除图片
const deleteImage = (index) => {
  const imgArray = form.value.images.split(',')
  imgArray.splice(index, 1)
  form.value.images = imgArray.join(',')
}

const onClickLeft = () => history.back()
</script>

<style scoped>
.exception-page {
  min-height: 100vh;
  background-color: #f7f8fa;
}

.custom-nav {
  background: linear-gradient(135deg, #4fc08d, #42b983);
}

.custom-nav :deep(.van-nav-bar__title) {
  color: white;
}

.custom-nav :deep(.van-nav-bar__text),
.custom-nav :deep(.van-icon) {
  color: white !important;
}

.content-wrapper {
  padding: 16px;
}

.section-title {
  margin: 0 0 8px;
  font-size: 16px;
  font-weight: 600;
  color: #323233;
}

.upload-tip {
  margin: 0 0 16px;
  font-size: 14px;
  color: #969799;
}

.upload-section {
  background: white;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 16px;
}

.upload-trigger {
  width: 80px;
  height: 80px;
  background: #f7f8fa;
  border: 1px dashed #dcdee0;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.upload-trigger span {
  margin-top: 4px;
  font-size: 12px;
  color: #969799;
}

.image-preview {
  margin-top: 16px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
  gap: 8px;
}

.image-item {
  position: relative;
  width: 80px;
  height: 80px;
  opacity: 0;
  animation: fadeIn 0.3s ease forwards;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.fade-in {
  animation: fadeIn 0.3s ease forwards;
}

.image-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.delete-btn {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 20px;
  height: 20px;
  background-color: rgba(0, 0, 0, 0.6);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s;
}

.delete-btn:hover {
  background-color: rgba(0, 0, 0, 0.8);
  transform: scale(1.1);
}

.delete-btn .van-icon {
  color: white;
  font-size: 14px;
}

.form-section {
  background: white;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 16px;
}

.submit-section {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 16px;
  background: white;
  box-shadow: 0 -2px 8px rgba(0,0,0,0.05);
}

.van-button {
  height: 44px;
  border-radius: 22px;
  font-size: 16px;
}

:deep(.van-field__word-limit) {
  color: #969799;
}
</style>